#Thanks to Anvil from #fedora-fr for help on this makefile
help: 
	@echo "You must specify a target:"
	@echo "    make cgi:          to make cgi"
	@echo "    make microserver: to make micro_server application"
	@echo "    make modules:      compile each modules"
	@echo "    make clean:        cleanup project"
	@echo "    make install:      not yet implemented"
	

modules: $(foreach f,$(shell find ./modules -maxdepth 1 -name .svn -prune -o -type d -print| awk -F"/" '/modules\// {print $3}'),$(f).so)

core:
	@echo $(shell echo -e '\033[35mGenerate core library\033[0m' )
	pcc -d 1 -O -l core `find ./libs -name \*.php`
	@echo $(shell echo -e '\033[32mDone\033[0m' )

cgi: core modules
	@echo $(shell echo -e '\033[35mCompile FastCGI application\033[0m' )
	pcc -d 1 -O --not-found index.php --fastcgi -L ./ -u core pholow index.php resource.php
	@echo $(shell echo -e '\033[32mDone\033[0m' )

microserver: core modules
	@echo $(shell echo -e '\033[35mCompile Microserver application\033[0m' )
	pcc -d 1 -O --not-found index.php -s pholow -L ./ -u core index.php resource.php
	@echo $(shell echo -e '\033[32mDone\033[0m' )

%.so:
	@echo $(shell echo -e '\033[35mCompile $*\033[0m' )
	find $* -name \*.php | xargs -n 250 pcc -d 1 -O -l $$(basename $*)
	[[ -f libphp-$$(basename `find . -type f -name "lib$$(basename $*)*.so"`) ]] || ln -s $$(basename `find . -type f -name "lib$$(basename $*)*.so"`) libphp-$$(basename `find . -type f -name "lib$$(basename $*)*.so"`) 
	@echo $(shell echo -e '\033[32mDone\033[0m' )

clean:
	@echo $(shell echo -e '\033[35mCleaning...\033[0m' )
	rm -f `find ./ -name "*.so"`
	rm -f `find ./ -name "*.a"`
	rm -f `find ./ -name "*.sch"`
	rm -f `find ./ -name "*.heap"`
	rm -f `find ./ -name "*.o"`
	rm -f `find ./ -name "*.fcgi"`
	rm -f `find ./ -name "*_u.c"`
	rm -f `find ./ -name "pholow"`
	@echo $(shell echo -e '\033[32mDone\033[0m' )
